import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import DBuitls;
import User;


@WebServlet("/login")
public class LoginSerlvet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		String user = req.getParameter("user");
		String pwd = req.getParameter("pwd");
		QueryRunner qr = new QueryRunner();
		try {
			List<User> list = qr.query(DBuitls.getConn(), "select * from login", new BeanListHandler<User>(User.class));
			for (User u : list) {
				User u2 = new User();
				u2.setId(u.getId());
				u2.setUsername(u.getUsername());
				u2.setPwd(u.getPwd());
				if(user.equals(u2.getUsername()) && pwd.equals(u2.getPwd())){
					System.out.println(111);
					req.setAttribute("message", "成功");
					req.getRequestDispatcher("home1.jsp").forward(req, resp);
				}
			}
			req.setAttribute("message", "失败");
			req.getRequestDispatcher("password.jsp").forward(req, resp);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
